<template>
  <div class="ww100 mt30">
    <div class="">
      <el-form
        size="small"
        :inline="true"
        :model="formInline"
        class="demo-form-inline"
      >
        <el-form-item label="时间选择">
          <div class="block">
            <span class="demonstration"></span>
            <el-date-picker
              size="small"
              v-model="searchForm.createTime"
              type="daterange"
              value-format="YYYY-MM-DD"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="选择商户">
          <el-select
            size="small"
            v-model="formInline.supplierId"
            filterable
            placeholder="全部"
          >
            <el-option label="全部" value="0"></el-option>
            <el-option
              v-for="(item, index) in supplierList"
              :key="index"
              :label="item.name"
              :value="item.shopSupplierId"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="Search" @click="onSubmit"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <!--内容-->
    <div class="product-content">
      <div class="table-wrap">
        <el-table
          size="small"
          :data="tableData"
          border
          style="width: 100%"
          v-loading="loading"
        >
          <el-table-column
            prop="shopSupplierId"
            label="商户ID"
            width="90"
          ></el-table-column>
          <el-table-column prop="name" label="商户名称"></el-table-column>
          <el-table-column prop="orderMoney" label="订单总额"></el-table-column>
          <el-table-column
            prop="realSupplierMoney"
            label="商户实际结算金额"
          ></el-table-column>
          <el-table-column prop="realSysMoney" label="服务费"></el-table-column>
          <el-table-column prop="agentMoney" label="分销佣金"></el-table-column>
          <el-table-column fixed="right" label="操作">
            <template #default="scope">
              <el-button @click="detail(scope.row)" type="text" size="small"
                >详情</el-button
              >
              <el-button @click="onExport(scope.row)" type="text" size="small"
                >导出</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!--分页-->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          :current-page="curPage"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="totalDataNumber"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import SupplierApi from "@/api/supplier.js";
import { formatDate } from "@/utils/DateTime.js";
import qs from "qs";
import { useUserStore } from "@/store";
const { token } = useUserStore();
export default {
  components: {},
  data() {
    return {
      token,
      /*是否加载完成*/
      loading: true,
      /*列表数据*/
      tableData: [],
      /*一页多少条*/
      pageSize: 20,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      /*横向表单数据模型*/
      formInline: {
        supplierId: null,
      },
      searchForm: {
        createTime: [],
      },
      supplierList: [],
      /*是否打开添加弹窗*/
      open_add: false,
      /*是否打开编辑弹窗*/
      open_edit: false,
      /*当前编辑的对象*/
      userModel: {},
      logoImgs: [],
      businessImgs: [],
    };
  },
  created() {
    /*获取列表*/
    this.getTableList();
  },
  methods: {
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.curPage = val;
      self.loading = true;
      self.getTableList();
    },

    /*每页多少条*/
    handleSizeChange(val) {
      this.curPage = 1;
      this.pageSize = val;
      this.getTableList();
    },

    /*获取列表*/
    getTableList() {
      let self = this;
      let search = self.searchForm;
      let Params = self.formInline;
      Params.pageIndex = self.curPage;
      Params.pageSize = self.pageSize;
      if (search.createTime.length > 1) {
        Params.startDate = search.createTime[0];
        Params.endDate = search.createTime[1];
      } else {
        Params.startDate = new Date();
        Params.endDate = new Date();
        Params.startDate.setTime(
          Params.startDate.getTime() - 3600 * 1000 * 24 * 7
        );
        Params.startDate = formatDate(Params.startDate, "yyyy-MM-dd");
        Params.endDate = formatDate(Params.endDate, "yyyy-MM-dd");
        search.createTime[0] = Params.startDate;
        search.createTime[1] = Params.endDate;
      }
      SupplierApi.dataList(Params, true)
        .then((res) => {
          self.loading = false;
          self.tableData = res.data.dataList.records;
          self.totalDataNumber = res.data.dataList.total;
          self.supplierList = res.data.supplierList;
        })
        .catch((error) => {
          self.loading = false;
        });
    },
    /*打开添加*/
    detail(row) {
      let self = this;
      self.$router.push({
        path: "/cash/settled/dataDetail",
        query: {
          supplierId: row.shopSupplierId,
          pageIndex: self.curPage,
          pageSize: self.pageSize,
          startDate: self.searchForm.createTime[0],
          endDate: self.searchForm.createTime[1],
        },
      });
    },
    onExport(row) {
      const Params = {
        supplierId: row.shopSupplierId,
        pageIndex: 1,
        pageSize: 999999,
        startDate: this.searchForm.createTime[0],
        endDate: this.searchForm.createTime[1],
        tokenshop: this.token,
      };
      let baseUrl = import.meta.env.VITE_BASIC_URL;
      if (!baseUrl) {
        baseUrl = window.location.protocol + "//" + window.location.host;
      }
      //let baseUrl = "http://127.0.0.1:8890";
      window.location.href =
        baseUrl +
        "/api/shop/cash/settled/supplierDataExport?" +
        qs.stringify(Params);
    },
    /*搜索查询*/
    onSubmit() {
      this.curPage = 1;
      this.getTableList();
    },
  },
};
</script>

<style></style>
